#ifndef EDIT_SONG_FEATURE_H
#define EDIT_SONG_FEATURE_H

#include "feature.h"
#include "../widgets/gui_frame.h"
#include "../widgets/clickable_label.h"
#include "../widgets/button.h"
#include "../widgets/ps_slider.h"
#include "../widgets/list.h"
#include "../widgets/textbox.h"
#include "../widgets/player_controls.h"

#include "../../anime/utils.h"
#include "../../model/player.h"

#include <QPixmap>
#include <QObject>

class EditSongFeature: public Feature,
                       private ClickableLabelListener,
                       private AnimationObserver,
                       private SliderListener,
                       private ListListener,
                       private ButtonListener,
                       private PlayerControlsListener
{
    Q_OBJECT

public:
    EditSongFeature(int _id, Engine* _engine, int _x, int _y, int _w, int _h,
                  ControllerProxy* _cProxy, MainWidget* _mw);
    virtual ~EditSongFeature();

    virtual void Paint(QPainter &painter);

public slots:
    void TextChanged();

protected:

    virtual void Hide() { doHide(); }
    virtual void doShow();
    virtual void doHide();

    virtual void OnShow(Widget* w);
    virtual void OnHide(Widget *w);

    virtual void OnMouseClick( QMouseEvent *e, int x_rel, int y_rel );

private:
    enum EditMode { MenuMode, GeneralInfo, PSEditMode, LoopEditMode};
    EditMode currentMode;

    ClickableLabel* generalInfoLabel;
    ClickableLabel* psEditLabel;
    ClickableLabel* loopEditLabel;

    IntGraph animeGraph;
    Animation showMenuAnime;
    int generalInfoX;
    int psEditLabelX;
    int loopEditLabelX;

    int generalInfoX_id;
    int psEditLabelX_id;
    int loopEditLabelX_id;

    bool showingGeneralInfo;
    bool showingPsEditor;
    bool showingLoopEditor;
    bool hiding;
    Animation editMenuShowAnime;

    int generalInfoY_id;
    int psEditLabelY_id;
    int loopEditLabelY_id;
    int generalInfoTx_id;
    int psEditLabelTx_id;
    int loopEditLabelTx_id;

    OneMinus_NI<float> om_ni;
    bool reshowingMenu;

    int headerLineLength;

    // general info widget
    Label* infoFixedLabel[7];
    Label* infoLabel[7];
    ClickableLabel* saveLabel;
    ClickableLabel* editPsLabel;
    bool switchingToPsEditMode;
    ClickableLabel* editLoopLabel;
    bool switchingToLoopMode;
    Slider* pitchSlider;

    // ps editor widgets
    List* psList;
    Label* psFixedLabel[6];
    TextBox* psNameBox;
    QTime tmpTime;
    Label* psStartLabel;
    Label* psEndLabel;
    Label* psLengthLabel;
    Label* psSpeedLabel;
    Slider* psSpeedSlider;
    Label* psPitchLabel;
    Slider* psPitchSlider;
    Label* psTitleLabel;
    PSSlider* psPlayerSlider;
    Label* psTimeLabel;
    bool playing;
    bool updateSlider;
    Button* psAddButton;
    Button* psRemoveButton;
    Button* psInsertButton;
    Button* psClearButton;
    Button* psSetStartButton;
    Button* psSetEndButton;
    PlayerControls* psControls;
    Label* psInPlayFixedLabel;
    Label* psInPlayLabel;
    Button* psSaveButton;
    bool ignoreTextChange;

    Song* song;

    // loop editor widgets
    Label* lpActiveFixedLabel;
    QPixmap* onPixmap;
    QPixmap* offPixmap;
    QRect lpActiveRect;
    Label* lpActiveLabel;

    // ClickableLabelListener implementation
    virtual void OnClick(ClickableLabel* sender);

    // PlayerControlsListener implementation
    virtual void OnControlClick( PlayerControls* sender, int controlId );
    virtual void OnVolumeChange( PlayerControls* sender, float vol );

    // SliderListener implementation
    virtual void OnPosChanged( Slider* sender, double newPos );
    virtual void OnClickOrOnEndDrag( Slider* sender, double newPos );
    virtual void OnStartDrag( Slider* slider );

    // ListListener implementation
    virtual void OnSelect( List* sender, int index );
    virtual void OnDoubleClick( List* sender, int index );

    // ButtonListener implementation
    virtual void OnClick(Button* b, QMouseEvent* e, int rel_x, int rel_y);

    // AnimationObserver implementation
    virtual void OnPause(Animation* a);
    virtual void OnStop(Animation* a);
    virtual void OnEnd(Animation* a);
    virtual void OnPlay(Animation* a);

    // initialization functions
    void BuildWidgets();
    void BuildAnimations();

    // animations support functions
    void UnderlineText(QPainter& painter, ClickableLabel* l, int* px);

    // editors load and unload functions
    void LoadGeneralInfo();
    void LoadPSEditor();
    void LoadLoopEditor();

    void UnloadGeneralInfo();
    void UnloadPSEditor();
    void UnloadLoopEditor();

    void UnloadDone();

    // other functions
    void SynchronizeWidgetsWithSong( int elementToSelect = -1 );
};

#endif // EDIT_SONG_FEATURE_H
